/**
 * @author Haidar Osman
 */
var ViewManager = function(container, width, height) {
	this.views = [];
	this.classes = [];
	this.packages = [];
	this.relations = new RelationObjArray();
	this.currentView = null;
	var that = this;
	this.stage = new Kinetic.Stage(container, width, height);

	this.addView = function(name) {
		if(this.currentView !== null) {
			this.currentView.hide();
		}
		this.currentView = new UMLCanvas(this.stage, name, that,that.relations);
		this.views[this.views.length] = this.currentView;
		// this.currentView.show();
		this.currentView.show();
		addView(name);
	}
	$('#view_jqxTree').bind('select', function(event) {
		var selectedItem = $('#view_jqxTree').jqxTree('selectedItem');
		that.switchView(selectedItem.label);
	});
	this.addView("MainView");
	this.addClass = function(newClass) {
		// alert("adding");
		if(this.getClass(newClass.classObj.name) == null) {
			this.classes[this.classes.length] = newClass.classObj;
			newClass.group.visible = true;
			this.currentView.addClass(newClass);
			addItem(newClass.headerText.text);
		} else {
			alert("class already exists");
		}
	}

	this.addRelation = function(fromClassObj, toClassObj, type) {
		// this.relations[this.relations.length] = this.currentView.addRelation(fromClass, toClass, type);
		this.relations.addRelation(fromClassObj,toClassObj,type);
	}
	this.getClass = function(className) {
		var i = 0;
		for( i = 0; i < that.classes.length; i++) {
			if(that.classes[i].name == className) {
				return that.classes[i];
			}
		}
		return null;
	}
	this.removeRelation = function(fromClassObj, toClassObj) {
		that.relations.removeRelation(fromClassObj,toClassObj);
	}

	this.removeClass = function(className) {
		for( i = 0; i < that.classes.length; i++) {
			if(that.classes[i].name == className) {
				var j=0;
				var fromClasses=[];
				var toClasses=[];
				// alert("found "+that.classes[i].name);
				for(j=0;j<that.relations.members.length;j++)
				{
					// alert("removing relations");
					if(that.relations.members[j].fromClass.name==className)
					{
						fromClasses[fromClasses.length]=that.relations.members[j].fromClass;
						toClasses[toClasses.length]=that.relations.members[j].toClass;
					}
					if(that.relations.members[j].toClass.name==className)
					{
						fromClasses[fromClasses.length]=that.relations.members[j].fromClass;
						toClasses[toClasses.length]=that.relations.members[j].toClass;
					}
				}
				for(j=0;j<fromClasses.length;j++)
				{
					
					that.relations.removeRelation(fromClasses[j],toClasses[j]);
				}
				
				// var j = 0;
				// for( j = 0; j < that.classes[i].outgoingRelations.members.length; j++) {
					// that.removeRelation(that.classes[i].outgoingRelations.members[j].fromClass, that.classes[i].outgoingRelations.members[j].toClass);
				// }
				// for( j = 0; j < that.classes[i].incomingRelations.members.length; j++) {
					// that.removeRelation(that.classes[i].incomingRelations.members[j].fromClass, that.classes[i].incomingRelations.members[j].toClass);
				// }
				for(j=0;j<that.views.length;j++)
				{
					// alert("removing from "+that.views[j].name);
					that.views[j].removeClass(that.classes[i].name);
				}
				that.classes.splice(i, 1);
				removeItem(className);
				break;
			}
		}
	}

	this.update = function() {
		that.currentView.update();
	}
	this.switchView = function(name) {
		var i = 0;
		for( i = 0; i < that.views.length; i++) {
			if(that.views[i].name == name) {
				that.currentView.hide();
				that.currentView = that.views[i];
				that.currentView.show();
				that.currentView.update();
				//alert("changes");
				break;
			}
		}
	}
	// Some tree events . . .

	var contextMenu = $("#jqxMenu").jqxMenu({
		width : '120px',
		theme : 'darkblue',
		height : '56px',
		autoOpenPopup : false,
		mode : 'popup'
	});
	// disable the default browser's context menu.
	$(document).bind('contextmenu', function(e) {
		if($(e.target).parents('.jqx-tree').length > 0) {
			return false;
		}
		return true;
	});
	function isRightClick(event) {
		var rightclick;
		if(!event)
			var event = window.event;
		if(event.which)
			rightclick = (event.which == 3);
		else if(event.button)
			rightclick = (event.button == 2);
		return rightclick;
	}

	var clickedItem = null;
	// open the context menu when the user presses the mouse right button.
	$("#project_jqxTree li").live('mousedown', function(event) {
		var target = $(event.target).parents('li:first')[0];
		$("#project_jqxTree").jqxTree('selectItem', target);

		var rightClick = isRightClick(event);
		if(rightClick) {
			var scrollTop = $(window).scrollTop();
			var scrollLeft = $(window).scrollLeft();

			contextMenu.jqxMenu('open', parseInt(event.clientX) + 5 + scrollLeft, parseInt(event.clientY) + 5 + scrollTop);
			return false;
		}
	});
	// function trim(s) {
		// var l = 0;
		// var r = s.length - 1;
		// while((l < s.length && s[l] == ' ')||(l < s.length && s[l] == '\n')) {
			// l++;
		// }
		// while((r > l && s[r] == ' ')||(r > l && s[r] == '\n')) {
			// r -= 1;
		// }
		// return s.substring(l, r + 1);
	// }

	String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); };
	$("#jqxMenu").live('itemclick', function(event) {
		var item =$(event.args).text().trim();
		
		// alert("l"+item+"r");
		switch (item) {
			case "Add To View":
				var selectedItem = $('#project_jqxTree').jqxTree('selectedItem');
				// alert(selectedItem.label);
				that.currentView.addClassObj(that.getClass(selectedItem.label));
				break;
			case "Remove":
				var selectedItem = $('#project_jqxTree').jqxTree('selectedItem');
				// alert("class "+selectedItem.label);
				that.removeClass(selectedItem.label);
				that.currentView.update();
				that.currentView.updateRelations();
				if(selectedItem != null) {
					$('#project_jqxTree').jqxTree('removeItem', selectedItem.element);
				}
				break;
			default:
				alert("default");
				break;
		}
	});
}